package com.hzbat.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzbat.entity.IbmsOS;
import com.hzbat.entity.RegisterHost;
import com.hzbat.entity.WorkerHost;
import com.hzbat.service.FSHttpService;
import com.hzbat.service.RegisterNodeConfiguereService;

@Service("registerNodeConfiguereServiceImpl")
public class RegisterNodeConfiguereServiceImpl implements RegisterNodeConfiguereService {

	private final String URI_OS_TYPES_LIST = "ostypesList";
	private final String URI_HOSTS_LIST    = "hostsList" ;
	private final String URI_WORKERS_LIST  = "workersList";
	
	@Autowired
	FSHttpService fSHttpServiceImpl ;
	
	/***
	 * 获取注册中操作系统信息
	 */
	public List<IbmsOS> getIbmsOSList(IbmsOS ibmsOs) {
		List<IbmsOS> list = new ArrayList<IbmsOS>() ;
		String result = getListByURI(URI_OS_TYPES_LIST);
		list = parseIbmsOSResponse(result,3);
		return list;
	}


	/***
	 * 
	 * 获取注册节点列表
	 * 
	 */
	public List<RegisterHost> getRegisterHostList(RegisterHost registerHost) {
		List<RegisterHost> list = new ArrayList<RegisterHost>();
		String result = getListByURI(URI_HOSTS_LIST);
		list = parseRegisterHostResponse(result,6);
		return list;
	}

	
	/***
	 * 
	 * 获取计算节点进程列表
	 * 
	 */
	public List<WorkerHost> getWorkerHostList(WorkerHost workerHost) {
		List<WorkerHost> list = new ArrayList<WorkerHost>();
		String result = getListByURI(URI_WORKERS_LIST);
		list = parseWorkerHostResponse(result,7);
		return list;
	}
	
    /***
     * 解析OS返回报文
     * @param result
     * @param length
     * @return
     */
	private List<IbmsOS> parseIbmsOSResponse(String result,int length) {
		Pattern pattern = Pattern.compile("<td>.*</td>");
		Matcher matcher = pattern.matcher(result);
		List<IbmsOS> list = new ArrayList<IbmsOS>();
		IbmsOS os = new IbmsOS();
		int flag = length ;
		while(matcher.find()){
				String text = matcher.group();
				text = text.substring(4, text.length()-5);
				switch(flag--){
					case 3 : 
						os.setSystemName(text);
						break;
					case 2 : 
						os.setRelease(text);
						break;
					case 1 : 
						os.setBits(text);
						list.add(os);
						os = new IbmsOS();
						break;
				}
		}
		return list;
	}
	
    /***
     * 解析Host返回报文
     * @param result
     * @param length
     * @return
     */
	private List<RegisterHost> parseRegisterHostResponse(String result,int length) {
		Pattern pattern = Pattern.compile("<td>.*</td>");
		Matcher matcher = pattern.matcher(result);
		List<RegisterHost> list = new ArrayList<RegisterHost>();
		RegisterHost host = new RegisterHost();
		int flag = length ;
		while(matcher.find()){
				String text = matcher.group();
				text = text.substring(4, text.length()-5);
				switch(flag--){
					case 6 : 
						host.setSystemName(text);
						break;
					case 5 : 
						host.setRelease(text);
						break;
					case 4 : 
						host.setBits(text);
						break;
					case 3 : 
						host.setIp(text);
						break;
					case 2 : 
						host.setIdlerCount(Integer.valueOf(text));
						break;
					case 1 : 
						flag = length ;
						host.setWorkingCount(Integer.valueOf(text));
						list.add(host);
						host = new RegisterHost();
						break;
				}
		}
		return list;
	}

    /***
     * 解析Worker返回报文
     * @param result
     * @param length
     * @return
     */
	private List<WorkerHost> parseWorkerHostResponse(String result,int length) {
		Pattern pattern = Pattern.compile("<td>.*</td>");
		Matcher matcher = pattern.matcher(result);
		List<WorkerHost> list = new ArrayList<WorkerHost>();
		WorkerHost worker = new WorkerHost();
		int flag = length ;
		while(matcher.find()){
				String text = matcher.group();
				text = text.substring(4, text.length()-5);
				switch(flag--){
					case 7 : 
						worker.setSystemName(text);
						break;
					case 6 : 
						worker.setRelease(text);
						break;
					case 5 : 
						worker.setBits(text);
						break;
					case 4 : 
						worker.setIp(text);
						break;
					case 3 : 
						worker.setPort(text);
						break;
					case 2 : 
						worker.setIsWorking(Integer.valueOf(text));;
						break;
					case 1 : 
						flag = length ;
						worker.setProgramAndParams(text);
						list.add(worker);
						worker = new WorkerHost();
						break;						
				}
		}
		return list;
	}	
	
	
	public String getListByURI(String URI){
		String url = "http://158.1.0.55:11001/"+URI;
		String result = fSHttpServiceImpl.sendGetRequest(url);
		//System.out.println("=====: "+result);
		return result ;
	}

}
